package com.xxkk.spring_boot_web.service;

import com.alibaba.fastjson.JSONObject;
import com.xxkk.spring_boot_web.module.OrderState;
import com.xxkk.spring_boot_web.sql.mapper.RechargeOrderMapper;
import com.xxkk.spring_boot_web.sql.mapper.RechargeTopPayMapper;
import com.xxkk.spring_boot_web.sql.pojo.RechargeOrder;
import com.xxkk.spring_boot_web.sql.pojo.RechargeOrderExample;
import com.xxkk.spring_boot_web.sql.pojo.RechargeTopPay;
import com.xxkk.spring_boot_web.sql.pojo.RechargeTopPayExample;
import com.xxkk.spring_boot_web.utils.TimeUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class SqlService {
    @Autowired
    private RechargeTopPayMapper topPayCollectionTableMapper;//TopPay下单表SQL映射
    @Autowired
    private RechargeOrderMapper rechargeOrderMapper;
    private final Logger log = Logger.getLogger(SqlService.class);

    public boolean checkTopPayDayOrderNum(String roleId) {
        Date the_day_of_today_zero_hour_time = TimeUtil.getTodayZeroHourDate();
        RechargeTopPayExample example = new RechargeTopPayExample();
        example.createCriteria()
                .andRoleIdEqualTo(roleId)
                .andCreateTimeGreaterThan(the_day_of_today_zero_hour_time);
        long sum_ = this.topPayCollectionTableMapper.countByExample(example);
        return sum_ > 100;
    }

    /**
     * 获取玩家今日充值次数
     *
     * @param roleId 玩家id
     * @return 返回玩家今日充值次数
     */
    public long getRechargeTodayTimes(String roleId) {
        Date the_day_of_today_zero_hour_time = TimeUtil.getTodayZeroHourDate();
        RechargeOrderExample example = new RechargeOrderExample();
        example.createCriteria()
                .andRoleIdEqualTo(roleId)
                .andCreateTimeGreaterThan(the_day_of_today_zero_hour_time);
        return this.rechargeOrderMapper.countByExample(example);
    }


    public int insertTopPayCollectionTable(RechargeTopPay table) {
        return this.topPayCollectionTableMapper.insertSelective(table);
    }

    public int addOrder(RechargeOrder order) {
        return this.rechargeOrderMapper.insertSelective(order);
    }

    public int updateOrder(RechargeOrder order) {
        RechargeOrderExample em = new RechargeOrderExample();
        em.createCriteria().andMchOrderNumEqualTo(order.getMchOrderNum());
        return this.rechargeOrderMapper.updateByExampleSelective(order, em);
    }


    public int updateTPOrder(RechargeTopPay table) {
        RechargeTopPayExample em = new RechargeTopPayExample();
        em.createCriteria().andMchOrderNumEqualTo(table.getMchOrderNum());
        return this.topPayCollectionTableMapper.updateByExampleSelective(table, em);
    }

    public RechargeTopPay getTPOrder(String mchtOrderNum) {
        RechargeTopPayExample em = new RechargeTopPayExample();
        em.createCriteria().andMchOrderNumEqualTo(mchtOrderNum);
        List<RechargeTopPay> ls = this.topPayCollectionTableMapper.selectByExample(em);
        return ls.isEmpty() ? null : ls.get(ls.size() - 1);
    }

    public RechargeOrder getOrder(String mchtOrderNum) {
        RechargeOrderExample em = new RechargeOrderExample();
        em.createCriteria().andMchOrderNumEqualTo(mchtOrderNum);
        List<RechargeOrder> ls = this.rechargeOrderMapper.selectByExample(em);
        return ls.isEmpty() ? null : ls.get(ls.size() - 1);
    }

    public boolean isFirstRecharge(String roleId) {
        RechargeOrderExample em = new RechargeOrderExample();
        em.createCriteria().andRoleIdEqualTo(roleId).andStateEqualTo(OrderState.SUCCESS.name());
        List<RechargeOrder> ls = this.rechargeOrderMapper.selectByExample(em);
        return ls.isEmpty();
    }


}
